package com.example.laciudadentusp;  

import android.app.Activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter; // Para hacer el marcador modificable
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

// GPS
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.content.Context;

public class MainActivity extends Activity implements LocationListener{

	LocationManager locationManager ;
	//static final String  provider ="gps";
	String  provider;

	
	//static final LatLng HAMBURG = new LatLng(53.558, 9.927);
	//static final LatLng KIEL = new LatLng(53.551, 9.993);
	//new LatLng(-16.416877,-71.513894)
	private GoogleMap map;

	private TextView latituteField;
	private TextView longitudeField;
	double latituteActual;
	double longitudeActual;
	


	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//setContentView(R.layout.layoutgps);

		latituteField = (TextView) findViewById(R.id.TextView02);
		longitudeField = (TextView) findViewById(R.id.TextView04);


		//-------- Buscar la posicion mediante GPS----- //
        // Getting LocationManager object
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);        
        
        // Creating an empty criteria object
        Criteria criteria = new Criteria();
        
        // Getting the name of the provider that meets the criteria
        provider = locationManager.getBestProvider(criteria, false);
   
                
        if(provider!=null && !provider.equals("")){
        	
        	// Get the location from the given provider
            Location location = locationManager.getLastKnownLocation(provider);
            Toast.makeText(getBaseContext(), "El proveedor es:" + provider, Toast.LENGTH_SHORT).show();
            // milisegundos y metros 
            locationManager.requestLocationUpdates(provider, 10000, 1, this);
            
            
            if(location!=null)
            	onLocationChanged(location);
            else
            	Toast.makeText(getBaseContext(), "La locacion no puede ser recuperado", Toast.LENGTH_SHORT).show();
            
        }else{
        	Toast.makeText(getBaseContext(), "No se encuentra proveedor", Toast.LENGTH_SHORT).show();
        }
		
		//------- Graficar los iconos ------------//
	    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

	    map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

	    // Creamos un nuevo marcador de mi position actual
	    Marcador objMarc = new Marcador("CineMark", "MALL AVENTURA PLAZA AREQUIPA Av. " +
	    		"Porongoche #500 (Ex. Hipodromo Porongoche) Paucarpata - Arequipa", R.drawable.cinemark, new LatLng(-16.416877,-71.513894) );

	    Marcador position = new Marcador("CineMark", "Mi posicion", R.drawable.posicion, new LatLng(latituteActual,-71.513894) );


	    map.addMarker(new MarkerOptions()
	        .position(position.getCoodMarc())
	        .title(position.getNomb())
	        .snippet(position.getDescripMarc())
	        .icon(BitmapDescriptorFactory.fromResource(position.getNombIcono2())));

	    map.addMarker(new MarkerOptions()
        .position(objMarc.getCoodMarc())
        .title(objMarc.getNomb())
        .snippet(objMarc.getDescripMarc())
        .icon(BitmapDescriptorFactory.fromResource(objMarc.getNombIcono2())));

	    
	    // Move the camera instantly to hamburg with a zoom of 15.
	    map.moveCamera(CameraUpdateFactory.newLatLngZoom(objMarc.getCoodMarc(), 20));

	    // Zoom in, animating the camera.
	    map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);

	    map.setInfoWindowAdapter(new InfoWindowAdapter() {

	    	  @Override
	           public View getInfoWindow(Marker marker) {
	               return null;
	           }

	    	  @Override
	           public View getInfoContents(Marker marker) {
	               View myContentsView = getLayoutInflater().inflate(R.layout.marcador, null);
	                   TextView tvTitle = ((TextView)myContentsView.findViewById(R.id.title));
	                   tvTitle.setText(marker.getTitle());
	                   TextView tvSnippet = ((TextView)myContentsView.findViewById(R.id.snippet));
	                   tvSnippet.setText(marker.getSnippet());
	               return myContentsView;
	           }
	       });
	 
	}


	/* Request updates at startup */
	@Override
	protected void onResume() {
		super.onResume();
	}

	/* Remove the locationlistener updates when Activity is paused */
	@Override
	protected void onPause() {
		super.onPause();
	}
	
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
	@Override
	public void onLocationChanged(Location location) {
		// Getting reference to TextView tv_longitude
		TextView tvLongitude = (TextView)findViewById(R.id.textViewLongitud);
		longitudeActual = location.getLongitude();
		
		// Getting reference to TextView tv_latitude
		TextView tvLatitude = (TextView)findViewById(R.id.textViewLatitud);
		latituteActual =location.getLatitude();
		
		// Setting Current Longitude
		tvLongitude.setText("Longitude:" + location.getLongitude());	
		// Setting Current Latitude
		tvLatitude.setText("Latitude:" + location.getLatitude() );
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub		
	}

}

